package com.windoent.yugioh.ui.activity.registlogin;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.AppManager;
import com.windoent.yugioh.MainActivity;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.bean.VerifyQQBean;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.LogUtils;
import com.windoent.yugioh.utils.SharedPreUtil;
import com.windoent.yugioh.utils.SimpleUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.PhoneCode;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.lang.ref.WeakReference;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/4/27 17:58
 * desc   :获取验证码
 */
public class ObtainCellPhoneCodeActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout llJump;
    private TextView tvCellPhone;
    private TextView tvCellPhoneModify;
    private PhoneCode phoneCode;
    private TextView textGetCode;
    private LinearLayout llReload;
    private String cell_phone;
    private View mView;


    @Override
    public int getLayout() {
        return R.layout.activity_obtain_cell_phone;
    }

    @Override
    public void initView() {
        super.initView();
        AppManager.getAppManager().addActivity(this);
        mView = (View) findViewById(R.id.view);
        immersiveStatusBar(mView);
        llJump = (LinearLayout) findViewById(R.id.ll_jump);
        tvCellPhone = (TextView) findViewById(R.id.tv_cell_phone);
        tvCellPhoneModify = (TextView) findViewById(R.id.tv_cell_phone_modify);
        phoneCode = (PhoneCode) findViewById(R.id.phone_code);
        textGetCode = (TextView) findViewById(R.id.btn_get_code);
        llReload = (LinearLayout) findViewById(R.id.ll_reload);

    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        cell_phone = intent.getStringExtra("cell_phone");
        tvCellPhone.setText("+86\u0020" + cell_phone);
        startTimer();
    }

    @Override
    public void initListener() {
        super.initListener();
        llJump.setOnClickListener(this);
        tvCellPhoneModify.setOnClickListener(this);
        llReload.setOnClickListener(this);
        phoneCode.setOnInputListener(new PhoneCode.OnInputListener() {
            @Override
            public void onSucess(String code) {
                //成功,跳转
                verifyCellPhoneCode(cell_phone, code);
            }

            @Override
            public void onInput(String phoneCode) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.ll_jump:
                startActivity(new Intent(mContext, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.tv_cell_phone_modify:
                finish();
                break;
            case R.id.ll_reload:
                getCellPhoneCode(cell_phone);
                startTimer();
                break;
            default:
                break;
        }
    }

    //开始倒计时
    private void startTimer() {
        llReload.setEnabled(false);
        SimpleUtils.startTimer(new WeakReference<TextView>(textGetCode), "60s", 60, 1, new SimpleUtils.TimerListener() {
            @Override
            public void onFinished() {
                llReload.setEnabled(true);
            }
        });
    }

    //获取手机号验证码
    private void getCellPhoneCode(String cell) {
        showLoadingDialog("");
        PhoneLive.getCellPhoneCode(cell, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                LogUtils.d("response-->" + response);
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            startTimer();
                            GeneralBean generalBean = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(generalBean.getMsg());
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    //验证手机短信
    private void verifyCellPhoneCode(final String bind_cell, final String code) {
        showLoadingDialog("");
        PhoneLive.verifyLoginEmailCode(bind_cell, "tel", code, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                LogUtils.d("responseveri-->" + response);
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            VerifyQQBean bean = gson.fromJson(response, VerifyQQBean.class);
                            ToastUtils.showText(bean.getMsg());
                            VerifyQQBean.DataBean data = bean.getData();
                            if (data != null && data.isFirst()) {
                                String token = data.getToken();
                                SharedPreUtil.put(mContext, Constants.APPTOKEN, token);
                                Intent intent = new Intent(mContext, CompletePersonInfoActivity.class);
                                intent.putExtra("token", token);
                                startActivity(intent);
                            } else if (data != null) {
                                String token = data.getToken();
                                SharedPreUtil.put(mContext, Constants.APPTOKEN, token);
                                startActivity(new Intent(mContext, MainActivity.class));
                                AppManager.getAppManager().finishAllActivity();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "登录注册获取手机号验证码界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "登录注册获取手机号验证码界面");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
